<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

try {
  $hostname = HOST_NAME;
  $username = USER_NAME2;
  $password = PASSWORD2;
  $dbname = DATABASE_NAME2;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}


//$orm1 = classToOrm('produceselect');
try {
  $stmt = $myPdo->prepare("SELECT t.e_oid AS id, t.trackId FROM ns_ProduceTrack AS t
                          ");
  $stmt->execute();
  
  //$myManager->start_t();
  $myPdo->beginTransaction();
  $i = 0;

  while ($i < $stmt->rowCount()) {
    $track = $stmt->fetch(PDO::FETCH_OBJ);
    
    $sql1 = " SELECT DISTINCT u.e_oid as id, u.trackId,
              u.account as account, u.password, u.name,
              u.email, u.type
              FROM ns_ProduceUser AS u LEFT JOIN ns__ez_relation_producetrack_produceuser AS pu ON (pu.oid_b = u.e_oid AND pu.var_a = 'preconUsers') where pu.oid_a = $track->id
            ";
    $stmt1 = $myPdo->prepare($sql1);
    $stmt1->execute();
    $m = 0;
    while ($m < $stmt1->rowCount()) {
      $tmp1 = $stmt1->fetch(PDO::FETCH_OBJ);
      $sql2 = "INSERT INTO ns_ProduceTrackUser (trackId, track_trackId, account, password, name, email, type) VALUES ('$tmp1->trackId', '$track->trackId', '$tmp1->account', '$tmp1->password', '$tmp1->name', '$tmp1->email', '$tmp1->type')";
      $myPdo->exec($sql2);
      $m++;
    }
    $i++;
  }
  $myPdo->commit();
  echo json_encode(array());
} catch (PDOException $e) {
  $myPdo->rollBack();
  echo $e->getMessage();
}
?>